﻿using AnesSystem.Models;
using AnesSystem.Services;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    public class DoctorOptViewModelOne : BaseViewModel<DoctorOptViewModelOne>
    {

        #region Variables   

        private string _zmysAccount = "";
        private string _fmys1Account = "";
        private string _fmys2Account = "";
        private string _zmDate = "Hidden";

        private string _zmysRole = "";

        private string _fmys1Role = "";

        private string _fmys2Role = "";


        private String _disAnesDoctorName = "";



        #endregion


        #region Constructor
        public DoctorOptViewModelOne()
        {
            if (PatientLink.AnesthesiaRecord.Zmys != null && PatientLink.AnesthesiaRecord.Zmys.Length == 24) _zmysAccount = PatientLink.AnesthesiaRecord.Zmys;
            if (PatientLink.AnesthesiaRecord.Fmys1 != null && PatientLink.AnesthesiaRecord.Fmys1.Length == 24) _fmys1Account = PatientLink.AnesthesiaRecord.Fmys1;
            if (PatientLink.AnesthesiaRecord.Fmys2 != null && PatientLink.AnesthesiaRecord.Fmys2.Length == 24) _fmys2Account = PatientLink.AnesthesiaRecord.Fmys2;
            if (PatientLink.AnesthesiaRecord.Zmys != null && PatientLink.AnesthesiaRecord.Zmys.Length == 24)
            {
                AccountModel a = DataManager.Instance.AccountList.ToList().Find(O => O.Id.ToString().Equals(PatientLink.AnesthesiaRecord.Zmys));
                if (a != null) ZmysRole = a.Title;
            }
            if (PatientLink.AnesthesiaRecord.Fmys1 != null && PatientLink.AnesthesiaRecord.Fmys1.Length == 24)
            {
                AccountModel a = DataManager.Instance.AccountList.ToList().Find(O => O.Id.ToString().Equals(PatientLink.AnesthesiaRecord.Fmys1));
                if (a != null) Fmys1Role = a.Title;
            }
            if (PatientLink.AnesthesiaRecord.Fmys2 != null && PatientLink.AnesthesiaRecord.Fmys2.Length == 24)
            {
                AccountModel a = DataManager.Instance.AccountList.ToList().Find(O => O.Id.ToString().Equals(PatientLink.AnesthesiaRecord.Fmys2));
                if (a != null) Fmys2Role = a.Title;
            }
        }
        #endregion


        #region Properties


        #endregion

        AccountModel aa = new AccountModel();
        #region 依赖属性
        public string ZmysAccount
        {
            get
            {
                return _zmysAccount;
            }

            set
            {
                _zmysAccount = value;
                if (_zmysAccount != null)
                {
                    aa = DataManager.Instance.AccountList.ToList().Find(O =>
                    {
                        if (!string.IsNullOrEmpty(O.Id.ToString()))
                            return O.Id.ToString().Equals(_zmysAccount);
                        return false;
                    });
                    if (aa != null) ZmysRole = aa.Title;
                }
                RaisePropertyChanged("ZmysAccount");
            }
        }

        public string Fmys1Account
        {
            get
            {
                return _fmys1Account;
            }

            set
            {
                _fmys1Account = value;
                if (_fmys1Account != null)
                {
                    aa = DataManager.Instance.AccountList.ToList().Find(O =>
                    {
                        if (!string.IsNullOrEmpty(O.Id.ToString()))
                            return O.Id.ToString().Equals(_fmys1Account);
                        return false;
                    });
                    if (aa != null) Fmys1Role = aa.Title;
                }
                RaisePropertyChanged("Fmys1Account");
            }
        }

        public string Fmys2Account
        {
            get
            {
                return _fmys2Account;
            }

            set
            {
                _fmys2Account = value;
                if (_fmys2Account != null)
                {
                    aa = DataManager.Instance.AccountList.ToList().Find(O =>
                    {
                        if (!string.IsNullOrEmpty(O.Id.ToString()))
                            return O.Id.ToString().Equals(_fmys2Account.ToString());
                        return false;
                    });
                    if (aa != null) Fmys2Role = aa.Title;
                }
                RaisePropertyChanged("Fmys2Account");
            }
        }

        public string ZmDate
        {
            get
            {
                return _zmDate;
            }

            set
            {
                _zmDate = value; RaisePropertyChanged("ZmDate");
            }
        }

        public string DisAnesDoctorName
        {
            get
            {
                ConvertAnesDoctor();
                if (string.IsNullOrEmpty(_disAnesDoctorName)) return "";
                return _disAnesDoctorName.Substring(0, _disAnesDoctorName.Length - 2);
            }

            set
            {
                _disAnesDoctorName = value;
                RaisePropertyChanged("DisAnesDoctorName");
            }
        }

        public string Fmys1Role
        {
            get
            {
                return _fmys1Role;
            }

            set
            {
                _fmys1Role = value;
                RaisePropertyChanged("Fmys1Role");
            }
        }

        public string Fmys2Role
        {
            get
            {



                return _fmys2Role;
            }

            set
            {
                _fmys2Role = value;
                RaisePropertyChanged("Fmys2Role");
            }
        }

        public string ZmysRole
        {
            get
            {
                return _zmysRole;
            }

            set
            {
                _zmysRole = value;
                RaisePropertyChanged("ZmysRole");
            }
        }
        #endregion


        #region Event


        #endregion


        #region PrivateMethods
        private void ConvertAnesDoctor()
        {
            _disAnesDoctorName = "";
            if (PatientLink.AnesthesiaRecord.Zmys != null && PatientLink.AnesthesiaRecord.Zmys.Length == 24)
            {
                AccountModel a = DataManager.Instance.AccountList.ToList().Find(O => O.Id.ToString().Equals(PatientLink.AnesthesiaRecord.Zmys));
                if (a != null) _disAnesDoctorName += a.TrueName + "(" + a.Title + "), ";
            }
            if (PatientLink.AnesthesiaRecord.Fmys1 != null && PatientLink.AnesthesiaRecord.Fmys1.Length == 24)
            {
                AccountModel a = DataManager.Instance.AccountList.ToList().Find(O => O.Id.ToString().Equals(PatientLink.AnesthesiaRecord.Fmys1));
                if (a != null) _disAnesDoctorName += a.TrueName + "(" + a.Title + "), ";
            }
            if (PatientLink.AnesthesiaRecord.Fmys2 != null && PatientLink.AnesthesiaRecord.Fmys2.Length == 24)
            {
                AccountModel a = DataManager.Instance.AccountList.ToList().Find(O => O.Id.ToString().Equals(PatientLink.AnesthesiaRecord.Fmys2));
                if (a != null) _disAnesDoctorName += a.TrueName + "(" + a.Title + "), ";
            }
        }


        #endregion


        #region Public/ProtectedMethods
        public void Keep()
        {
            ZmysAccount = Util.ExtensionMethods.CheckAccount(_zmysAccount, "麻醉医生");
            PatientLink.AnesthesiaRecord.Zmys = ZmysAccount;

            Fmys1Account = Util.ExtensionMethods.CheckAccount(_fmys1Account, "麻醉医生");
            PatientLink.AnesthesiaRecord.Fmys1 = Fmys1Account;

            Fmys2Account = Util.ExtensionMethods.CheckAccount(_fmys2Account, "麻醉医生");
            PatientLink.AnesthesiaRecord.Fmys2 = Fmys2Account;


            AccountModel a = FindRoleById(_zmysAccount);
            if (a != null)
            {
                a.Title = _zmysRole;
                AccountService.Instance.AddUpdate(a);
            }
            AccountModel b = FindRoleById(_fmys1Account);
            if (b != null)
            {
                b.Title = _fmys1Role; AccountService.Instance.AddUpdate(b);
            }
            AccountModel c = FindRoleById(_fmys2Account);
            if (c != null)
            {
                c.Title = _fmys2Role; AccountService.Instance.AddUpdate(c);
            }
            PatientLinkService.Instance.AddUpdate(PatientLink);
            RaisePropertyChanged("DisAnesDoctorName");
        }

        
        private AccountModel FindRoleById(string st)
        {
            AccountModel c = null;
            if (!string.IsNullOrEmpty(st))
                c = DataManager.Instance.AccountList.ToList().Find(O => O.Id.ToString().Equals(st));
            return c;
        }
        #endregion


        #region EventHandlers



        #endregion

    }
}
